import 'dart:math';

import 'package:easy_rich_text/easy_rich_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/vip_support_model.dart';
import 'package:xvideos/hj_page/pre_sale/pre_sale_provider.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';

class VipPrivilegeView extends StatelessWidget {
  final VipProductModel? model;
  const VipPrivilegeView({super.key, this.model});

  @override
  Widget build(BuildContext context) {
    if (model?.newPrivilege?.isEmpty ?? true) return SizedBox.shrink();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        6.sizeBoxH,
        Text(
          '会员特权',
          style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
        ),
        18.sizeBoxH,
        GridView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
            mainAxisSpacing: 12,
            crossAxisSpacing: 16,
            childAspectRatio: 72 / 108,
          ),
          itemCount: model!.newPrivilege!.length,
          itemBuilder: (BuildContext context, int index) {
            final privilege = model!.newPrivilege![index];
            return Column(
              children: [
                Expanded(
                  child: LayoutBuilder(
                    builder: (BuildContext context, BoxConstraints constraints) {
                      final size = min(constraints.maxHeight, constraints.maxWidth);
                      return NetworkImageLoader(
                        imageUrl: privilege.img ?? '',
                        width: size,
                        height: size,
                        borderRadius: 90,
                      );
                    },
                  ),
                ),
                4.sizeBoxW,
                Text(
                  "${(privilege.privilegeDesc?.split(" ").length ?? 0) > 1 ? privilege.privilegeDesc?.split(" ")[0].trim() : privilege.privilegeDesc?.split("\n")[0]}",
                  maxLines: 1,
                  style: TextStyle(
                    color: Color(0xffADADAD),
                    fontWeight: FontWeight.w400,
                    fontSize: 11,
                  ),
                  textAlign: TextAlign.center,
                ),
                Text(
                  "${(privilege.privilegeDesc?.split(" ").length ?? 0) > 1 ? privilege.privilegeDesc?.split(" ")[1].trim() : (privilege.privilegeDesc?.split("\n").length ?? 0) > 1 ? privilege.privilegeDesc?.split("\n")[1] : ""}",
                  maxLines: 1,
                  style: TextStyle(
                    color: Color(0xffADADAD),
                    fontWeight: FontWeight.w400,
                    fontSize: 11,
                  ),
                  textAlign: TextAlign.center,
                ),
              ],
            );
          },
        ),
        26.sizeBoxH,
        Divider(
          height: 1,
          color: Colors.white.withOpacity(.1),
        ),
        26.sizeBoxH,
      ],
    );
  }
}

class VIPProductItem extends StatelessWidget {
  final bool isSelect;
  final VipProductModel model;
  final Function()? onTap;

  const VIPProductItem(this.model, {super.key, this.isSelect = false, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
        return Stack(
          clipBehavior: Clip.none,
          alignment: Alignment.bottomCenter,
          children: [
            Container(
              width: double.infinity,
              height: double.infinity,
              decoration: !model.isPreSale
                  ? BoxDecoration(
                      color: isSelect ? null : Color(0xff202133),
                      borderRadius: BorderRadius.circular(9),
                      gradient: isSelect
                          ? LinearGradient(
                              colors: [
                                Color(0xffFFEEDC),
                                Color(0xffEEC7B2),
                                Color(0xffFADDC1),
                              ],
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                            )
                          : null)
                  : null,
              child: !model.isPreSale
                  ? Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          model.productName ?? '',
                          style: TextStyle(color: isSelect ? Color(0xff241110) : Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
                        ),
                        2.sizeBoxH,
                        EasyRichText(
                          '¥${(model.discountedPrice ?? 0) ~/ 10}',
                          textAlign: TextAlign.center,
                          patternList: [
                            EasyRichTextPattern(
                                targetString: '¥',
                                style: TextStyle(
                                    color: isSelect ? Color(0xff241110) : Colors.white, fontSize: 18, fontWeight: FontWeight.w500))
                          ],
                          defaultStyle:
                              TextStyle(color: isSelect ? Color(0xff241110) : Colors.white, fontSize: 32, fontWeight: FontWeight.w500),
                        ),
                        2.sizeBoxH,
                        Text(
                          '¥${(model.originalPrice ?? 0) ~/ 10}',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              decoration: TextDecoration.lineThrough,
                              decorationColor: isSelect ? Color(0xff241110) : Color(0xff999999),
                              color: isSelect ? Color(0xff241110) : Color(0xff999999),
                              fontSize: 12,
                              fontWeight: FontWeight.w500),
                        ),
                        2.sizeBoxH,
                        Text(
                          model.vipCardDesc ?? '',
                          textAlign: TextAlign.center,
                          maxLines: 1,
                          style:
                              TextStyle(color: isSelect ? Color(0xff241110) : Color(0xff999999), fontSize: 12, fontWeight: FontWeight.w500),
                        ),
                      ],
                    )
                  : Stack(
                      alignment: Alignment.center,
                      children: [
                        Opacity(
                            opacity: isSelect ? 1 : 0,
                            child: NetworkImageLoader(
                              imageUrl: model.realSelectVipImage(),
                              width: 140,
                              height: 180,
                              fit: BoxFit.cover,
                              borderRadius: 12.r,
                            )),
                        Opacity(
                            opacity: isSelect ? 0 : 1,
                            child: NetworkImageLoader(
                              imageUrl: model.realNormalVipImage(),
                              width: 140,
                              height: 180,
                              fit: BoxFit.cover,
                              borderRadius: 12,
                            )),
                        if (PreSaleProvider().canPayBalance())
                          Positioned(
                            bottom: 2,
                            child: ValueListenableBuilder(
                              valueListenable: PreSaleProvider().deadline,
                              builder: (BuildContext context, String value, Widget? child) {
                                return Text(
                                  '剩余时间  $value',
                                  style: TextStyle(color: Colors.white, fontSize: 8, fontWeight: FontWeight.w400),
                                );
                              },
                            ),
                          )
                      ],
                    ),
            ),

            // 会员卡描述
            if (model.actionDesc?.isNotEmpty ?? false)
              Positioned(
                top: -8,
                left: 0,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 13),
                  height: 22,
                  alignment: Alignment.centerLeft,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [Color(0xffEABFA0), Color(0xffF2D3B9)]),
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(12.r),
                      bottomRight: Radius.circular(12.r),
                    ),
                  ),
                  child: ConstrainedBox(
                    constraints: BoxConstraints(maxWidth: constraints.maxWidth - 30),
                    child: Text(
                      model.actionDesc ?? '',
                      maxLines: 1,
                      style: TextStyle(fontSize: 11.sp, color: Color(0xff141010), fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
              ),
          ],
        );
      }),
      onTap: () => onTap?.call(),
    );
  }
}
